<?php

/*
 * Copyright 2012 Clement Sannicolo <clement.sannicolo@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace CSan\NutrientBundle\Entity;

use Doctrine\ORM\Mapping as ORM,
    Doctrine\Common\Collections\ArrayCollection;

use Symfony\Component\Validator\Constraints as Assert;

use Gedmo\Mapping\Annotation as Gedmo;

use CSan\NutrientBundle\Entity\Food; 

/**
 * @ORM\Entity(repositoryClass="CSan\NutrientBundle\Entity\CategoryRepository")
 * @ORM\Table(
 *   name="nutrient_category",
 *   indexes={@ORM\Index(name="public_id_index", columns={"public_id"})}
 * )
 * @Gedmo\Tree(type="nested")
 */
class Category
{
	/**
	 * @ORM\Id
	 * @ORM\Column(type="integer")
	 * @ORM\GeneratedValue(strategy="AUTO")
	 */
	protected $id;

	/**
	 * @ORM\Column(name="public_id", type="string", length=32)
	 */
	protected $publicId;

	/**
	 * @ORM\Column(type="string", length=255)
	 * @Assert\NotBlank()
	 * @Assert\MinLength(3)
	 * @Assert\MaxLength(255)
	 */
	protected $name;

	/**
	 * @ORM\Column(type="integer")
	 * @Gedmo\TreeLeft
	 */
	private $lft;

	/**
	 * @ORM\Column(type="integer")
	 * @Gedmo\TreeLevel
	 */
	private $lvl;

	/**
	 * @ORM\Column(type="integer")
	 * @Gedmo\TreeRight
	 */
	private $rgt;

	/**
	 * @ORM\Column(name="root_id", type="integer", nullable=true)
	 * @Gedmo\TreeRoot
	 */
	private $rootId;

	/**
	 * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
	 * @Gedmo\TreeParent
	 */
	private $parent;

	/**
	 * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"all"})
	 * @ORM\OrderBy({"lft" = "ASC"})
	 */
	private $children;

	/**
	 * @ORM\OneToMany(targetEntity="CSan\NutrientBundle\Entity\Food", mappedBy="category")
	 */
	protected $foods;

    /**
     * Constructor
     *
     * @param string $publicId
     */
	public function __construct($publicId)
	{
		$this->publicId = $publicId;
		$this->children = new ArrayCollection();
		$this->foods = new ArrayCollection();
	}

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set publicId
     *
     * @param string $publicId
     */
    public function setPublicId($publicId)
    {
        $this->publicId = $publicId;
    }

    /**
     * Get publicId
     *
     * @return string 
     */
    public function getPublicId()
    {
        return $this->publicId;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set rootId
     *
     * @param integer $rootId
     */
    public function setRootId($rootId)
    {
        $this->rootId = $rootId;
    }

    /**
     * Get rootId
     *
     * @return integer 
     */
    public function getRootId()
    {
        return $this->rootId;
    }

    /**
     * Set parent
     *
     * @param CSan\NutrientBundle\Entity\Category $parent
     */
    public function setParent(\CSan\NutrientBundle\Entity\Category $parent)
    {
        $this->parent = $parent;
    }

    /**
     * Get parent
     *
     * @return CSan\NutrientBundle\Entity\Category 
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add child
     *
     * @param CSan\NutrientBundle\Entity\Category $category
     */
    public function addChild(\CSan\NutrientBundle\Entity\Category $category)
    {
        $this->children[] = $category;
    }

    /**
     * Get children
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Add foods
     *
     * @param CSan\NutrientBundle\Entity\Food $food
     */
    public function addFood(\CSan\NutrientBundle\Entity\Food $food)
    {
        $this->foods[] = $food;
    }

    /**
     * Get foods
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getFoods()
    {
        return $this->foods;
    }
}